/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */


import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.swing.JOptionPane;

/**
 *
 * @author RICARDO ROCHA
 */
public class login extends HttpServlet {
   
    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();

        try {

             // usuario e senha corretos
              String usuario_p = "admin";
              String senha_p = "admin1234";

              // dados informados no formulário
              String usuario = request.getParameter("txtLogin");
              String senha = request.getParameter("txtSenha");

              // verifica se os dados informados estão corretos
              if((usuario.equals(usuario_p)) && (senha.equals(senha_p))){
                    response.setHeader("Refresh", 1+";url=Home.jsp");
                  out.println("Login efetuado com sucesso!");
              }
              else{
                // dados não conferem. Direcione o usuário para a página de
                // login novamente
                JOptionPane.showMessageDialog(null,"Dados não conferem. Tente novamente","Erro",JOptionPane.ERROR_MESSAGE);
              }
        } finally {
            out.close();
        }

    } 

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    } 

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }

   
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
